library(ggplot2)

nba_data<- read.csv('C:/Users/werol/Downloads/NBAstats/nba_stats.csv')
#head(nba_data)

nba_data_3 <- subset(nba_data, Year>1979)
#head(nba_data_3)

three_point <- ggplot(data = nba_data_3, mapping = aes(x = X3P.,y = X3PA, color = Player)) +
               geom_point(alpha = .5) +
               scale_colour_manual(values = c(
                "Stephen Curry" = "green", 
                "James Harden" = "red", 
                "Klay Thompson" = "blue")) +
               scale_alpha_manual(values = c(
                "Stephen Curry" = 1,
                "James Harden" = 1,
                "Klay Thompson" = 1)) +
               scale_size_manual(values = c(
                 "Stephen Curry" = 4,
                 "James Harden" = 4,
                 "Klay Thompson" = 4)) +
               xlab("3-point percentage") +
               ylab("3-point attempts") + 
               theme_classic() + 
               ggtitle("3-point Shooting Numbers for Every Player Across Every Season",
               subtitle = "Curry, Harden, and Thompson only players with multiple seasons above 625 attempts and 37.5% success rate") +                     
               scale_fill_grey()


three_point
## Warning: Removed 3511 rows containing missing values (geom_point).

three_point_year <- three_point + facet_wrap(~Year)
three_point_year
## Warning: Removed 3511 rows containing missing values (geom_point).

outliers <- subset(nba_data_3, X3PA>625, X3P.>.375)
outliers
##           X Year             Player Pos Age  Tm  G GS   MP  PER   TS. X3PAr
## 12592 12591 1996     George McCloud  SF  28 DAL 79 63 2846 15.9 0.543 0.529
## 12724 12723 1996       Dennis Scott  SF  27 ORL 82 82 3041 16.1 0.589 0.562
## 15974 15973 2002     Antoine Walker  PF  25 BOS 81 81 3406 17.1 0.490 0.382
## 17512 17511 2005 Quentin Richardson  SG  24 PHO 79 78 2839 13.6 0.522 0.604
## 17666 17665 2006          Ray Allen  SG  30 SEA 78 78 3022 22.2 0.590 0.435
## 23001 23000 2015      Stephen Curry  PG  26 GSW 80 80 2613 28.0 0.638 0.482
## 23634 23633 2016      Stephen Curry  PG  27 GSW 79 79 2700 31.5 0.669 0.554
## 23723 23722 2016       James Harden  SG  26 HOU 82 82 3125 25.3 0.598 0.406
## 24034 24033 2016      Klay Thompson  SG  25 GSW 80 80 2666 18.6 0.597 0.469
## 24217 24216 2017      Stephen Curry  PG  28 GSW 79 79 2638 24.6 0.624 0.547
## 24291 24290 2017        Eric Gordon  SG  28 HOU 75 15 2323 13.1 0.557 0.651
## 24307 24306 2017       James Harden  PG  27 HOU 81 81 2947 27.3 0.613 0.493
## 24613 24612 2017      Isaiah Thomas  PG  27 BOS 76 76 2569 26.5 0.625 0.439
## 24618 24617 2017      Klay Thompson  SG  26 GSW 78 78 2649 17.4 0.592 0.470
##         FTr ORB. DRB. TRB. AST. STL. BLK. TOV. USG. blanl  OWS  DWS   WS WS.48
## 12592 0.175  3.9 10.6  7.0 12.5  2.0  0.9 10.7 22.6    NA  4.2  1.6  5.8 0.098
## 12724 0.199  2.5  9.2  5.9 12.4  1.5  0.7  9.1 19.9    NA  6.4  2.4  8.8 0.138
## 15974 0.192  4.7 18.6 11.5 22.8  1.9  0.9 12.1 27.8    NA  1.5  5.2  6.7 0.094
## 17512 0.176  3.6 13.9  9.0  7.9  1.7  0.6  8.3 18.9    NA  3.3  2.5  5.8 0.098
## 17666 0.239  2.8 10.6  6.6 17.2  1.8  0.4 10.2 27.4    NA  9.8 -0.3  9.5 0.150
## 23001 0.251  2.4 11.4  7.0 38.6  3.0  0.5 14.3 28.9    NA 11.5  4.1 15.7 0.288
## 23634 0.250  2.9 13.6  8.6 33.7  3.0  0.4 12.9 32.6    NA 13.8  4.1 17.9 0.318
## 23723 0.518  2.2 15.6  8.8 35.4  2.2  1.4 15.9 32.5    NA 10.7  2.6 13.3 0.204
## 24034 0.159  1.5 10.3  6.2  9.8  1.1  1.4  8.5 26.3    NA  5.4  2.6  8.0 0.144
## 24217 0.251  2.7 11.4  7.3 31.1  2.6  0.5 13.0 30.1    NA  8.6  3.9 12.6 0.229
## 24291 0.172  1.4  8.1  4.7 12.3  1.0  1.4 10.0 22.1    NA  2.5  1.4  3.8 0.079
## 24307 0.575  3.5 20.9 12.2 50.7  2.0  1.0 19.5 34.2    NA 11.5  3.6 15.0 0.245
## 24613 0.441  1.9  7.0  4.4 32.6  1.4  0.4 10.7 34.0    NA 10.9  1.6 12.6 0.235
## 24618 0.158  2.2  9.2  5.9  9.3  1.2  1.2  8.0 26.1    NA  4.2  2.9  7.1 0.128
##       blank2 OBPM DBPM  BPM VORP  FG  FGA   FG. X3P X3PA  X3P. X2P X2PA  X2P.
## 12592     NA  2.6 -1.4  1.3  2.3 530 1281 0.414 257  678 0.379 273  603 0.453
## 12724     NA  3.6 -1.3  2.3  3.3 491 1117 0.440 267  628 0.425 224  489 0.458
## 15974     NA  1.8  1.2  3.0  4.3 666 1689 0.394 222  645 0.344 444 1044 0.425
## 17512     NA  2.0 -0.1  1.9  2.8 407 1045 0.389 226  631 0.358 181  414 0.437
## 17666     NA  6.7 -3.1  3.6  4.2 681 1500 0.454 269  653 0.412 412  847 0.486
## 23001     NA  9.6  0.3  9.9  7.9 653 1341 0.487 286  646 0.443 367  695 0.528
## 23634     NA 12.4  0.1 12.5  9.8 805 1598 0.504 402  886 0.454 403  712 0.566
## 23723     NA  7.1 -0.4  6.7  6.9 710 1617 0.439 236  657 0.359 474  960 0.494
## 24034     NA  2.9 -2.2  0.7  1.8 651 1386 0.470 276  650 0.425 375  736 0.510
## 24217     NA  7.7 -0.4  7.3  6.2 675 1443 0.468 324  789 0.411 351  654 0.537
## 24291     NA  1.0 -2.4 -1.4  0.4 412 1016 0.406 246  661 0.372 166  355 0.468
## 24307     NA  8.7  1.5 10.1  9.0 674 1533 0.440 262  756 0.347 412  777 0.530
## 24613     NA  8.8 -3.3  5.5  4.8 682 1473 0.463 245  646 0.379 437  827 0.528
## 24618     NA  2.4 -2.1  0.3  1.5 644 1376 0.468 268  647 0.414 376  729 0.516
##        eFG.  FT FTA   FT. ORB DRB TRB AST STL BLK TOV  PF  PTS
## 12592 0.514 180 224 0.804 116 263 379 212 113  38 166 212 1497
## 12724 0.559 182 222 0.820  63 246 309 243  90  29 122 169 1431
## 15974 0.460 240 324 0.741 150 564 714 407 122  38 251 237 1794
## 17512 0.498 136 184 0.739  91 388 479 158  96  27 102 197 1176
## 17666 0.544 324 359 0.903  71 261 332 286 105  16 188 151 1955
## 23001 0.594 308 337 0.914  56 285 341 619 163  16 249 158 1900
## 23634 0.630 363 400 0.908  68 362 430 527 169  15 262 161 2375
## 23723 0.512 720 837 0.860  63 438 501 612 139  51 374 229 2376
## 24034 0.569 193 221 0.873  35 271 306 166  60  49 138 152 1771
## 24217 0.580 325 362 0.898  61 292 353 523 143  17 239 183 1999
## 24291 0.527 147 175 0.840  29 172 201 188  48  40 121 150 1217
## 24307 0.525 746 881 0.847  95 564 659 906 120  37 464 215 2356
## 24613 0.546 590 649 0.909  43 162 205 449  70  13 210 167 2199
## 24618 0.565 186 218 0.853  49 236 285 160  66  40 128 139 1742
#install.packages("plotly")
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(htmlwidgets)
#three_point_year <- three_point + facet_wrap(~Year)
#three_point_year <- ggplotly(three_point_year)
#three_point_year
three_plot <- plot_ly(nba_data_3, 
        x = ~X3P., 
        y = ~X3PA, 
        type = 'scatter' 
        )

three_plot
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Warning: Ignoring 3511 observations
#plot_ly(
  #data = nba_data_3,
  #x = ~X3P.,
  #y = ~X3PA,
  #type = "scatter",
  #mode = "markers",
  #color = ~Player,
  #colors = c(ifelse("Stephen Curry"), "red", "black")
#)
nba_data_3$color <- ifelse(nba_data_3$Player == 'Stephen Curry', "red",
                           ifelse(nba_data_3$Player == 'Klay Thompson', "green",
                                  ifelse(nba_data_3$Player == 'James Harden', "blue", "black")))
                           
head(nba_data_3)
##         X Year               Player Pos Age  Tm  G GS   MP  PER   TS. X3PAr
## 5728 5727 1980 Kareem Abdul-Jabbar*   C  32 LAL 82 NA 3143 25.3 0.639 0.001
## 5729 5728 1980        Tom Abernethy  PF  25 GSW 67 NA 1222 11.0 0.511 0.003
## 5730 5729 1980          Alvan Adams   C  25 PHO 75 NA 2168 19.2 0.571 0.002
## 5731 5730 1980      Tiny Archibald*  PG  31 BOS 80 80 2864 15.3 0.574 0.023
## 5732 5731 1980        Dennis Awtrey   C  31 CHI 26 NA  560  7.4 0.524 0.000
## 5733 5732 1980           Gus Bailey  SG  28 WSB 20 NA  180  9.3 0.467 0.029
##        FTr ORB. DRB. TRB. AST. STL. BLK. TOV. USG. blanl OWS DWS   WS WS.48
## 5728 0.344  7.2 22.2 15.4 16.5  1.2  4.6 15.7 24.1    NA 9.5 5.3 14.8 0.227
## 5729 0.258  5.4 12.0  8.6  9.3  1.4  0.6  9.9 13.3    NA 1.2 0.8  2.0 0.080
## 5730 0.270  8.2 22.4 15.4 21.6  2.3  1.4 18.2 21.9    NA 3.1 3.9  7.0 0.155
## 5731 0.548  2.3  5.3  3.8 30.2  1.7  0.2 19.7 17.0    NA 5.9 2.9  8.9 0.148
## 5732 0.833  6.0 16.9 11.5  9.0  1.0  1.5 24.8  7.9    NA 0.1 0.5  0.6 0.053
## 5733 0.371  3.3 12.4  7.8 17.8  1.8  1.2 21.3 11.3    NA 0.0 0.2  0.2 0.043
##      blank2 OBPM DBPM  BPM VORP  FG  FGA   FG. X3P X3PA  X3P. X2P X2PA  X2P.
## 5728     NA  4.0  2.7  6.7  6.8 835 1383 0.604   0    1 0.000 835 1382 0.604
## 5729     NA -1.5 -0.1 -1.6  0.1 153  318 0.481   0    1 0.000 153  317 0.483
## 5730     NA  1.6  2.8  4.4  3.5 465  875 0.531   0    2 0.000 465  873 0.533
## 5731     NA  1.1 -1.1  0.0  1.5 383  794 0.482   4   18 0.222 379  776 0.488
## 5732     NA -2.9  1.5 -1.4  0.1  27   60 0.450   0    0    NA  27   60 0.450
## 5733     NA -3.7  1.3 -2.4  0.0  16   35 0.457   1    1 1.000  15   34 0.441
##       eFG.  FT FTA   FT. ORB DRB TRB AST STL BLK TOV  PF  PTS color
## 5728 0.604 364 476 0.765 190 696 886 371  81 280 297 216 2034 black
## 5729 0.481  56  82 0.683  62 129 191  87  35  12  39 118  362 black
## 5730 0.531 188 236 0.797 158 451 609 322 108  55 218 237 1118 black
## 5731 0.485 361 435 0.830  59 138 197 671 106  10 242 218 1131 black
## 5732 0.450  32  50 0.640  29  86 115  40  12  15  27  66   86 black
## 5733 0.471   5  13 0.385   6  22  28  26   7   4  11  18   38 black
#nba_data_3_color <- nba_data_3 %>% mutate(Color = case_when(Player == "Stephen Curry" ~ "red", Player != "Stephen Curry" ~ "black"))

plot_ly(
  data = nba_data_3,
  x = ~X3P.,
  y = ~X3PA,
  type = "scatter",
  mode = "markers",
  color = ~color,
  colors = c("red"="red", "green"="green", "blue"="blue", "black"="black"),
  hover_data = "text",
  text = ~Player,
  name = ifelse(nba_data_3$Player == "Stephen Curry", "Stephen Curry",
                ifelse(nba_data_3$Player == "Klay Thompson", "Klay Thompson",
                       ifelse(nba_data_3$Player == "James Harden", "James Harden", "other")))
)
## Warning: Ignoring 3511 observations
## Warning: 'scatter' objects don't have these attributes: 'hover_data'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'hover_data'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'hover_data'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'scatter' objects don't have these attributes: 'hover_data'
## Valid attributes include:
## 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
#nba_data_3$Color <- apply(FUN = if (nba_data_3$Player == "Stephen Curry") {"red"} else {"black"})